const webpack_1 = require("webpack");
class AuthorPlugin {
  constructor(options) {
    this.options = options;
  }
  apply(compiler) {
    compiler.hooks.compilation.tap("author-plugin", (compilation) => {
      compilation.hooks.processAssets.tap(
        {
          name: "WebpackObfuscator",
          stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING,
        },
        (assets) => {
          let options = this.options;
          let identifiersPrefixCounter = 0;
          let info = [];
          compilation.chunks.forEach((chunk) => {
            chunk.files.forEach((fileName) => {
              let { source } = assets[fileName].sourceAndMap();
              if (options.author) info.push(`@Author: ${options.author}`);
              if (options.email) info.push(`@Email: ${options.email}`);
              if (options.homepage) info.push(`@Homepage: ${options.homepage}`);
              if (info.length) {
                info.push(`@Date: ${new Date()}`);
                source = `/*\n  ${info.join("\n\n  ")}\n*/\n${source}`;
              }
              assets[fileName] = new webpack_1.sources.RawSource(source, false);
              identifiersPrefixCounter++;
            });
          });
        }
      );
    });
  }
}
module.exports = AuthorPlugin;
// wbepack4
// class AuthorPlugin {
//   constructor(options) {
//     this.options = options;
//   }
//   apply(compiler) {
//     compiler.hooks.emit.tap("author-plugin", (compilation) => {
//       const options = this.options;
//       return new Promise((resolve, reject) => {
//         const assets = compilation.assets;
//         Object.keys(assets).forEach((e) => {
//           let source = assets[e].source();
//           let info = [];

//           if (options.author) info.push(`@Author: ${options.author}`);
//           if (options.email) info.push(`@Email: ${options.email}`);
//           if (options.homepage) info.push(`@Homepage: ${options.homepage}`);

//           if (info.length) {
//             info.push(`@Date: ${new Date()}`);
//             source = `/*\n  ${info.join("\n\n  ")}\n*/\n${source}`;
//           }

//           compilation.assets[e].source = () => source;
//           compilation.assets[e].size = () => source.size;
//         });
//         resolve();
//       });
//     });
//   }
// }

// module.exports = AuthorPlugin;
